package com.cellulam.framework.core.log;

import com.cellulam.framework.core.utils.JacksonUtils;
import com.cellulam.framework.core.exceptions.SystemErrorStatus;
import com.cellulam.framework.core.auth.UserClient;
import com.cellulam.framework.core.log.enums.LogLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleLogClient implements LogClient {
    private static final Logger logger = LoggerFactory.getLogger(SimpleLogClient.class);

    @Override
    public void writeBizLog(BizLog log) {
        this.writeBizLog(UserClient.DEFAULT_USER_ID, log);
    }

    @Override
    public void writeBizLog(long userId, BizLog log) {
        logger.info("userId: {} - {}", userId, JacksonUtils.toJson(log));
    }

    @Override
    public void writeSystemLog(Exception ex) {
        this.writeSystemLog(ex, LogLevel.ERROR, SystemErrorStatus.SYS_ERROR.getStatus());
    }

    @Override
    public void writeSystemLog(Exception ex, LogLevel logLevel, int status) {
        this.writeSystemLog(UserClient.DEFAULT_USER_ID, logLevel, ex, status);
    }

    @Override
    public void writeSystemLog(long userId, LogLevel logLevel, Exception ex, int status) {
        switch (logLevel) {
            case DEBUG:
                logger.debug("userId: {}, status: {}, ex: {}", userId, status, ex);
                break;
            case INFO:
                logger.info("userId: {}, status: {}, ex: {}", userId, status, ex);
                break;
            case WARN:
                logger.warn("userId: {}, status: {}, ex: {}", userId, status, ex);
                break;
            case ERROR:
                logger.error("userId: {}, status: {}, ex: ", userId, status, ex);
                break;
            default:
                logger.info("userId: {}, status: {}, ex: {}", userId, status, ex);
        }
    }

    @Override
    public void writeSystemLog(SystemLog log) {
        this.writeSystemLog(UserClient.DEFAULT_USER_ID, log);
    }

    @Override
    public void writeSystemLog(long userId, SystemLog info) {
        logger.info("userId: {} - {}", userId, JacksonUtils.toJson(info));
    }

    @Override
    public void writeAccessLog(long userId, RequestHttpLog info) {
        logger.info("userId: {} - {}", userId, JacksonUtils.toJson(info));
    }

    @Override
    public void logSlowSql(SlowSqlLog log) {
        logger.info("{}", JacksonUtils.toJson(log));
    }

    @Override
    public void writeRestLog(RestLog log, Exception ex) {
        if(ex == null) {
            logger.debug("{}", JacksonUtils.toJson(log));
        }else {
            logger.error("{}", JacksonUtils.toJson(log), ex);
        }
    }
}
